package com.hf.www.base.controller;

import java.util.Date;

import com.hf.www.base.common.BaseController;
import com.hf.www.base.common.DisplayTool;
import com.hf.www.base.common.Pager;
import com.hf.www.base.model.SysRolesModel;
import com.hf.www.base.model.SysUserModel;
import com.hf.www.base.model.SysUserPostModel;
import com.hf.www.base.model.SysUserRoleModel;
import com.hf.www.core.plugin.annotation.Control;
import com.hf.www.tools.SysConstants;
import com.jfinal.aop.Before;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 用户
 * 
 *
 */
@Control(controllerKey = "/user")
public class UserController extends BaseController {

	private static final Logger LOG = Logger.getLogger(UserController.class);

	public void index() {
//		setAttr("posts", SysPostsModel.dao.allPosts());
		setAttr("rolesJson", JsonKit.toJson(SysRolesModel.dao.allRoles()));
		render("system/user");
	}
	public void list() {
		Pager pager = createPager("username" ,"keyword" , "status" ,"roleID");

		Page<?> page = SysUserModel.dao.page(pager);

		setAttr("total",page.getTotalRow());
		setAttr("rows",page.getList());
		renderJson();
	}
	
	
	public void toEdit(){
		Record editUser = SysUserModel.dao.getUserById(getParaToInt("id"));
		setAttr("editUser", editUser);
		setAttr("roles" , SysRolesModel.dao.allRoles());
		render("system/user_edit");
	}
	
	
	public void roleChecked() {
		Boolean checked = getParaToBoolean("checked");
		SysUserRoleModel userRole = getModelWithOutModelName(SysUserRoleModel.class, true);
		if (checked != null && userRole.get("roleID") != null && userRole.get("userID") != null) {
			setAttr(RESULT, true);
			SysUserRoleModel.dao.changeUserRole(userRole, checked);
			setAttr(MESSAGE, "修改成功！");
		} else {
			setAttr(RESULT, false);
			setAttr(MESSAGE, "参数错误，获取数据失败！");
		}
		renderJson();
	}
	
	public void save() {
		SysUserModel user = getModelWithOutModelName(SysUserModel.class, true);
		
		try {
			user.put("roles", getPara("roles"));
			SysUserModel.dao.editSave(user);
			setAttr("r", true);
			setAttr("m", "用户数据保存成功！");			
			setAttr("id", user.get("id"));
			DisplayTool.removeCache(DisplayTool.USER_KEY);
			
		} catch (Exception e) {
			
			setAttr("r", false);
			setAttr("m", "用户数据保存失败！");
			LOG.error(" 用户数据保存失败！" , e);
			
		}
		renderJson();
	}
	
	@Before(Tx.class)
	public void update() {
		SysUserModel user = getModelWithOutModelName(SysUserModel.class, true);
		if(user.get("id")==null){
			setAttr(RESULT, false);
			setAttr(MESSAGE, "更新失败！");
			renderJson();
			return;
		}
		user.set("updateDate", new Date());
		user.update();
		
		SysUserModel _user = getUser();
		if(user.get("id").equals(_user.get("id"))){//更新session
			_user.set("nick", user.get("nick"));
			_user.set("email", user.get("email"));
		}
		
//		modifyAuth(user);
		
		setAttr(RESULT, true);
		setAttr(MESSAGE, "更新成功！");
		DisplayTool.removeCache(DisplayTool.USER_KEY);
		renderJson();
	}
	
	/**
	 * 修改用户权限关联 当前权限仅由角色控制 且单用户单角色模式
	 * 
	 * 2014-11-19 弃用，用户编辑时不影响权限，权限单独分配
	 * 
	 * @Author Sean 2014-11-17 上午11:40:24
	 * @param user
	 */
	@Deprecated
	private void modifyAuth(SysUserModel user) {
		String roleID = getPara("roleID");
		String postID = getPara("postID");
		
		// 分配角色
		if(StrKit.notBlank(roleID)){
			SysUserRoleModel.dao.deleteByUser(user.get("id"));
			SysUserRoleModel userRole = new SysUserRoleModel();
			userRole.set("roleID",roleID);
			userRole.set("userID",user.get("id"));
			userRole.save();
		}
		// 分配岗位
		if(StrKit.notBlank(postID)){
			SysUserPostModel.dao.deleteByUser(user.get("id"));
			SysUserPostModel userPost = new SysUserPostModel();
			userPost.set("postID",postID);
			userPost.set("userID",user.get("id"));
			userPost.save();
		}
	}
	public void batchDel() {
		String ids = getPara("ids");

		try {
			SysUserModel.dao.batchDel(ids);
			setAttr("r" , true);
			setAttr("m" , "删除系统用户成功！");
		} catch (Exception e) {
			setAttr("r" , false);
			setAttr("m" ,  "删除系统用户失败");
			LOG.error("删除系统用户失败！" , e);
		}
		
		
		
		renderJson();
	}
	public void userMessage(){
		setAttr("user", SysUserModel.dao.findById(getUser().get("id")));
		render("tissue/user_message");
	}
	public void resetPassword(){
		SysUserModel user = new SysUserModel();
		String id = getPara("id");
		user.set("id", id);
		user.set("password", HashKit.md5(SysConstants.DEFAULT_PASSWORD));
		if (StrKit.notBlank(id) && user.update()) {
			setAttr(RESULT, true);
			setAttr(MESSAGE, "密码重置成功！");
		} else {
			setAttr(RESULT, false);
			setAttr(MESSAGE, "密码重置失败！");
		}
		renderJson();
	}

	public void password(){
		String oldPassword = getPara("oldPassword");
		String newPassword = getPara("newPassword");
		String entPassword = getPara("entPassword");

		boolean isError = true;
		if (StrKit.isBlank(oldPassword)) {
			setAttr(ERROR, "旧密码不能为空！");
		}else if (StrKit.isBlank(newPassword)) {
			setAttr(ERROR, "新密码不能为空！");
		}else if (StrKit.isBlank(entPassword)) {
			setAttr(ERROR, "确认密码不能为空！");
		}else if (!newPassword.equals(entPassword)) {
			setAttr(ERROR, "两次密码不相同！");
		}else{
			isError = false;
		}

		if(isError){
			renderJson();
			return;
		}

		SysUserModel user = getUser();
		String oldPass = HashKit.md5(oldPassword);
		if (!user.getStr("password").equals(oldPass)) {
			setAttr(ERROR, "旧密码错误！");
		} else {
			user.set("password",HashKit.md5(newPassword));
			if (user.update()) {
				setAttr(RESULT, true);
				setAttr(MESSAGE, "密码修改成功！");
			} else {
				setAttr(RESULT, false);
				setAttr(ERROR, "密码修改失败！");
			}
		}
		renderJson();
	}

}
